#include "sqlitedboperator.h"



SqliteDBAOperator::SqliteDBAOperator()
{
/*    QSqlDatabase database;
    if (QSqlDatabase::contains("qt_sql_default_connection"))
    {
        database = QSqlDatabase::database("qt_sql_default_connection");
    }
    else
    {
        //建立和sqlite数据的连接
        db = QSqlDatabase::addDatabase("QSQLITE");
        //设置数据库文件的名字
        QString dbname = QDir::currentPath() +QString("/")+ QString("EVENTTABLE.db");
        db.setDatabaseName(dbname);
    }*/

}
/*析构函数关闭数据库连接*/
SqliteDBAOperator::~SqliteDBAOperator()
{
    db.close();
}

//打开数据库
bool SqliteDBAOperator::OpenDb(void)
{
    //添加数据库驱动
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    //设置数据库名字（文件名）
    db.setDatabaseName("EVENTTABLE.db");
    //打开数据库
    if(db.open() == false){
        qDebug() << "Failed to connect to database";

        return false;
    }
    qDebug() << "Connected to database successfully";

    return true;
}

//判断数据库中某个数据表是否存在
bool SqliteDBAOperator::IsTaBexists(QString& Tabname)
{
    QSqlDatabase db = QSqlDatabase::database();
    if(db.tables().contains(Tabname))
    {
        qDebug()<<"Data table exists";
        return true;
    }
    qDebug()<<"Data table does not exist";
    return false;
}


//创建数据表
void SqliteDBAOperator::createTable(void)
{
    //用于执行sql语句的对象
    QSqlQuery query;
    //构建创建数据库的sql语句字符串

    QString str = "CREATE TABLE EVENTTABLE \
                          (\
                          ID  INTEGER PRIMARY KEY AUTOINCREMENT, \
                          DATE TEXT NOT NULL,\
                          TYPE TEXT NOT NULL,\
                          MESSAGE TEXT NOT NULL\
                          );";

    query.prepare(str);
    if(query.exec()== false)
    {
        qDebug()<<"The data table already exists";
    }
        else
    {
        qDebug()<<"Data table created successfully";
    }

}

void SqliteDBAOperator::addNewcolumn(QString& columnNameAndproperty)
{
}

//查询和显示结果
void SqliteDBAOperator::queryTable()
{
    QSqlQuery query;
    //构建创建数据库的sql语句字符串
    QString str = QString("SELECT ID,DATE,TYPE,MESSAGE FROM EVENTTABLE");
    //执行sql语句,并将结果集保存到model
    query.exec(str);

}


//单条插入
void SqliteDBAOperator::singleinsertdata(QString tablename ,w2dba &singledb)
{
    QSqlQuery query;
    QString str = QString(" INSERT INTO %1 values(null,'%2','%3','%4');"
                         ).arg(tablename).arg(singledb.date).arg(singledb.type).arg(singledb.message);
    if(query.exec(str) == false)
    {
        qDebug()<<"Insert the failure";

    }
    else
    {
        qDebug()<<"Insert the success";
    }
}


//多条插入
void SqliteDBAOperator::Moreinsertdata(QList<w2dba>& moredb)
{
    QSqlQuery query;
    query.prepare("insert into EVENTTABLE values (?,?,?,?,?,?,?,?)");
    QVariantList idlist,datelist,typelist,messagelsit;
    for(int i=0; i< moredb.size(); i++)
    {
        idlist <<  moredb.at(i).id;
        typelist << moredb.at(i).type;
        datelist << moredb.at(i).date;
        messagelsit << moredb.at(i).message;
    }
    query.addBindValue(idlist);
    query.addBindValue(datelist);
    query.addBindValue(typelist);
    query.addBindValue(messagelsit);

    if (!query.execBatch())
    {
        qDebug() << query.lastError();
    }

}


//删除一条数据
void SqliteDBAOperator::deletedata()
{

}
//修改
void SqliteDBAOperator::updatedata()
{
    //比较简单，自行补充
}


void SqliteDBAOperator::closeDb(void)
{
    db.close();
}
